<!--
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/1/3-16:21
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
*-->
{extend name='public/base' /}


{block name="content"}

<div class="layui-col-md12">

    <div class="layui-card">
        <div class="layui-card-body layui-row ">
            <div class="layui-card-header" style="line-height: 55px;">
                <div class="layui-btn-container do_btn">
                    <!--按钮区-->
                    <button class="layui-btn layui-btn-sm" data-type="add_menu"><i class="layui-icon"></i>新增菜单</button>
                    <button class="layui-btn layui-btn-sm layui-btn-primary" data-type="reload"><i class="fa fa-refresh"></i> 刷新</button>
                    <button class="layui-btn layui-btn-sm " id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-sm " id="btn-fold">全部折叠</button>

                </div>
            </div>

        </div>

    </div>

    <div class="layui-card">

        <div class="layui-card-body">

            <!--表格区-->

            <div class="yys-fluid yys-wrapper">
                <div class="layui-row lay-col-space20">
                    <div class="layui-cos-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12">
                        <section class="yys-body">
                            <div class="yys-body-content clearfix changepwd">
                                <div class="layui-col-lg12 layui-col-md10 layui-col-sm12 layui-col-xs12" style="width:100%">
                                    <div class="user-tables">
                                        <table id="tableFilter" lay-filter="tableFilter"> </table>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </div>
                </div>
            </div>


        </div>
    </div>

</div>


{/block}


{block name="js"} <!--js处理区-->


<!--模板-->
<script type="text/html" id="operationTpl">
    {:widget('common/forms/Addbtn',array('新增子菜单',2,'add'))}
    {:widget('common/forms/Editbtn',array('编辑菜单'))}
    {:widget('common/forms/Delbtn',array('删除菜单',2))}

</script>



<script type="text/html" id="status">
    <input type="checkbox" name="status"  lay-data="{{d.status}}" value="{{d.id}}"  lay-filter="sexDemo" lay-skin="switch"  lay-text="是|否"  {{ d.status == 1 ? 'checked' : '' }}>
</script>


<script>

    layui.use(['element', 'table', 'form', 'jquery', 'lucky','treetable'], function () {
        let element = layui.element;
        let form = layui.form;
        let table = layui.table;
        let $ = layui.jquery;
        let lucky=layui.lucky;
        let treetable = layui.treetable;

        form.render();

        String.prototype.format = function () {
            //字符串占位符
            //eg: let str1 = "hello {0}".format("world");
            if (arguments.length == 0) return this;
            let param = arguments[0];
            let s = this;
            if (typeof (param) == 'object') {
                for (let key in param) {
                    s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
                }
                return s;
            } else {
                for (let i = 0; i < arguments.length; i++) {
                    s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
                }
                return s;
            }
        };
        // layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#tableFilter',
            url:"{:url('menu/index')}",
            page: false,
            cols: [[
                {field: 'id',  title: 'ID',width:60},
                {field: 'title', minWidth: 100, title: '菜单名称'},
                {
                    field: 'icon', width: 70, align: 'center', templet: function (d) {
                        if (d.icon == "") {
                            return '';
                        } else {
                            let a= ('<i class="{0} {1} " ></i>').format(d.fontFamily,d.icon);
                            return a;
                        }
                    }, title: '图标'
                },
                {field:'is_menu',title:'是否显示',templet: '#status',width:90 ,unresize: true},
                {field: 'href',style:'cursor: pointer;',title: '菜单url',width:200,event: 'rehref',},
                {field: 'listorder',style:'cursor: pointer;',title: '排序',width:90,edit:'number',sort:true},
                {field: 'is_menu', title: '菜单按钮',width: 90,align: 'center',templet:function (rd) {
                        let s="";
                        if (rd.is_menu==1){
                            s='<button class="layui-btn layui-btn-xs ">菜单</button>';
                        } else {
                            s='<button class="layui-btn layui-btn-xs layui-btn-warm">按钮</button>';
                        }
                        return s;
                    }},
                {field: 'create_time', title: '添加时间',align: 'center',width:180,sort:true},
                {fixed: 'right',templet: '#operationTpl', width: 160, align: 'center', title: '操作'}
            ]],

            done: function (res) {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#tableFilter');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#tableFilter');
        });

        /**
         * 监听是否显示操作
         */
        form.on('switch(sexDemo)', function(obj){
            let _id=parseInt(obj.value);
            let pan=obj.elem.checked;
            let status;
            if (pan===false){
                status=2;
            }else {
                status=1;
            }
            lucky.Change_status("{:url('common/changeStatus')}","tableFilter","menu",_id,"status",status, obj.othis);
        });

        /**
         * 监听单元格编辑
         */
        table.on('edit(tableFilter)',function (obj) {
            let val=obj.value,_id=obj.data.id,field=obj.field;
            if (field=="href"){

            } else {
                let reg=/^[0-9]*$/;
                if (!reg.test(val)) {
                    layer.msg("排序必须是数字",{time:1000});
                    lucky.CreateReload("table_id");
                    return false;
                }
            }
            lucky.Change_status("{:url('common/changeStatus')}","tableFilter","menu",_id,field,val,obj.othis);
        });


        /**
         * 监听单行工具操作
         */
        table.on('tool(tableFilter)', function (obj) {
            let data = obj.data;
            // console.log(JSON.stringify(data));
            let _id=parseInt(data.id);
            let layEvent = obj.event;
            if (layEvent==="add"){
                let url="/admin/menu/addMenu/id/"+_id;
                lucky.CreateForm("添加子菜单",'55%','70%',url,"tableFilter",1);

            }else if(layEvent==="edit"){

                let urls="/admin/menu/editMenu/id/"+_id;
                lucky.CreateForm("编辑菜单",'55%','70%',urls,"tableFilter",1);

            }else if(layEvent==="del"){
                lucky.Delete_data(_id,"{:url('menu/del')}","tableFilter");
            }else if(layEvent==="rehref"){

                layer.prompt({
                    formType: 3
                    ,title: '修改地址'
                    ,value: data.href
                }, function(value, index){

                    $.post("{:url('common/changeStatus')}",{id:_id,table:'menu',field:'href',status:value},function (res) {
                        if (res.code==1){
                            layer.close(index);
                            layer.msg(res.msg,{kin: 'layui-layer-lan', icon:1,time:1000},function () {
                                //同步更新表格和缓存对应的值
                                obj.update({
                                    href: value
                                });
                            });
                        }else {
                            layer.msg(res.msg,{kin: 'layui-layer-lan', icon:5,time:1000},function () {
                                layer.close(index);
                            });
                        }
                    },'json');

                });
            }
        });


        let active = {
            add_menu:function(){
                lucky.CreateForm("添加菜单",'55%','70%',"{:url('menu/addMenu')}","tableFilter",1);
            },
            del: function(){ //获取选中数据
                let checkStatus = table.checkStatus('tableFilter'),data = checkStatus.data;
                let num=0;
                let id=[];
                for (let i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (num<1) {
                    layer.msg("请选择一项",{time:1500});return false;
                }
                lucky.Delete_data(id,"{:url('menu/del')}","tableFilter");
            },
            reload: function(){
                lucky.CreateReload("table_id");
            },
            move:function () {
                let checkStatus = table.checkStatus('tableFilter'),data = checkStatus.data;
                let num=0;
                let id=[];
                for (let i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (num<1) {
                    layer.msg("请选择一项",{time:1500});return false;
                }
                layer.msg("功能待开发",{time:1500});return false;
            }
        };

        $('.do_btn .layui-btn').on('click', function(){
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });



    });

</script >


{/block}
